package org.kuan.lab.common.service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.kuan.lab.base.service.BaseService;
import org.kuan.lab.common.entity.User;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * Project: erp<br>
 * Description: 用户管理Service，容器单例<br>
 * Copyright (c) 2012<br>
 * All Rights Reserved.<br>
 * @author <a href="mailto:gly420@163.com">GuanLuYong</a><br>
 */
@Service("userService") @Scope("singleton")
public class UserService extends BaseService<User> implements Serializable {
	private static final long serialVersionUID = 1L;
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void update(User t) throws Exception {
		super.update(t);
	}
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void insert(User t) throws Exception {
		super.insert(t);
	}
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void delete(User t) throws Exception {
		super.delete(t);
	}

	public List<User> queryUsers(Map<String, Object> conditions) {
		Query query = em.createQuery("SELECT u FROM User u WHERE u.code like :code and u.name like :name order by u.code");
		query.setParameter("code", (conditions.get("code") == null ? "%" : conditions.get("code").toString() + "%"));
		query.setParameter("name", (conditions.get("name") == null ? "%" : "%" + conditions.get("name").toString() + "%"));
		return query.getResultList();
	}
}
